<template>
  <CustomDialog v-model:visible="dialogVisible" top="8vh" curent-class="diner-dialog">
    <div class="diner-container">
      <div class="title">修改用餐人数</div>

      <div class="input-row">
        <span class="label">人数</span>
        <div class="input-container">
          <input class="input" v-model.trim="keyword" type="text" placeholder="请输入人数" />
          <img
            class="clear-icon"
            @click="handleClear"
            v-show="keyword"
            src="@/assets/images/home/input-clear.png"
            alt="清除"
          />
        </div>
      </div>

      <NumberKeyboard
        v-model:keyword="keyword"
        :showRadixPoint="false"
        :isFirstZero="false"
        :maxStrLimit="3"
        :loading="editUseNumLoading"
        @onInputKeyboard="onInputKeyboard"
        @onCancel="handleClear"
        @onConfirm="handleConfirm"
      />
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import NumberKeyboard from '@/components/NumberKeyboard/Index.vue'
import { deskEditUseNum } from '@/api/desktop'

interface Props {
  modelValue: boolean
  deskDetail?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
  (e: 'refreshDesk'): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  deskDetail: null
})

const emit = defineEmits<Emits>()

const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})

const keyword = ref('')
const editUseNumLoading = ref(false)

watch(
  () => props.modelValue,
  val => {
    if (val) {
      keyword.value = String(props.deskDetail?.use_num ?? '')
    } else {
      keyword.value = ''
    }
  }
)

const handleClear = () => {
  keyword.value = ''
}

const onInputKeyboard = (val: string) => {
  keyword.value = val
}

const handleConfirm = async () => {
  const recordId = props.deskDetail?.id
  if (!recordId) {
    ElMessage.warning('当前桌台信息异常')
    return
  }
  if (!keyword.value) {
    ElMessage.warning('请输入人数')
    return
  }
  const num = parseInt(keyword.value)
  if (isNaN(num) || num < 0) {
    ElMessage.warning('请输入有效的人数')
    return
  }
  // 桌台最大可容纳人数限制
  const capacity = Number(props.deskDetail?.desk?.capacity || 0)
  // if (capacity && num > capacity) {
  //   ElMessage.warning(`人数不能超过桌台最大容量（${capacity}人）`)
  //   return
  // }
  editUseNumLoading.value = true
  try {
    const res = await deskEditUseNum({ record_id: recordId, num: num })
    if (res.status === 200) {
      ElMessage.success(res.data?.message || '修改人数成功')
      dialogVisible.value = false
      emit('success')
      emit('refreshDesk')
    } else {
      ElMessage.error(res.data?.message || '修改人数失败')
    }
  } catch (e) {
    ElMessage.error('修改人数失败')
  } finally {
    editUseNumLoading.value = false
  }
}
</script>

<style scoped lang="scss">
.diner-container {
  box-sizing: border-box;
}

.title {
  font-size: 20px;
  font-family:
    PingFang SC,
    PingFang SC-Bold;
  font-weight: 700;
  text-align: center;
  color: #333333;
}

.input-row {
  display: flex;
  align-items: center;
  margin: 30px 0;
  .label {
    font-size: 16px;
    color: #333333;
    margin-right: 12px;
  }
  .input-container {
    display: flex;
    align-items: center;
    width: 452px;
    height: 45px;
    padding: 0px 12px;
    background: #ffffff;
    border: 1px solid #d2d2d2;
    border-radius: 8px;
    .input {
      outline: none;
      border: none;
      width: 100%;
      font-size: 16px;
      color: #333333;
    }
    .clear-icon {
      flex-shrink: 0;
      width: 16px;
      height: 16px;
      cursor: pointer;
      margin-left: 8px;
    }
  }
}
</style>
